namespace ACEMPortal.Administration.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Xml;

	using ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData;

	/// <summary>
	///		Summary description for UserRoleSelection.
	/// </summary>
	public partial class UserRoleSelection : System.Web.UI.UserControl
	{

		private string _XMLUsers;
		private string _XMLRoles;

		public string XMLUsers
		{
			get 
			{
				_XMLUsers = (string)ViewState["XMLUsers"];
				return _XMLUsers;
			}
			set 
			{
				_XMLUsers = value;
				ViewState["XMLUsers"] = _XMLUsers;
			}
		}

		public string XMLRoles
		{
			get 
			{
				_XMLRoles = (string)ViewState["XMLRoles"];
				return _XMLRoles;
			}
			set 
			{
				_XMLRoles = value;
				ViewState["XMLRoles"] = _XMLRoles;
			}
		}


		public void inizialize()
		{
			
			// cancella gli elementi dellalistbox
			lstAllRoles.Items.Clear();
			lstSelectedUsers.Items.Clear();

			// cancella gli elementi dellalistbox
			lstAllUsers.Items.Clear();
			lstSelectedRoles.Items.Clear();

		}

		public void BindUsersRoles()
		{
			// retrive users form viewstte
			
			XMLUsers SelectedUsers = new XMLUsers();
			SelectedUsers.Parse((string)ViewState["XMLUsers"]);

			// retrive roles form viewstte

			XMLRoles SelectedRoles = new XMLRoles();
			SelectedRoles.Parse((string)ViewState["XMLRoles"]);

			// fill general
			BindAllRoles();
			BindAllUsers();


			// fill listbox selectedUsers
			foreach (XMLUser currentUser in SelectedUsers) 
			{
				ListItem matchItem = lstAllUsers.Items.FindByValue(currentUser.Key.ToString());
				if (matchItem != null) 
				{
					lstSelectedUsers.Items.Add(matchItem);
					lstAllUsers.Items.Remove(matchItem);
				}
			}

			// fill listbox selectedRoles
			foreach (XMLRole currentRole in SelectedRoles) 
			{
				ListItem matchItem = this.lstAllRoles.Items.FindByValue(currentRole.Key.ToString());
				if (matchItem != null) 
				{
					lstSelectedRoles.Items.Add(matchItem);
					lstAllRoles.Items.Remove(matchItem);
				}
			}


		}

		private void BindAllRoles()
		{
			// Execute WAPI Command
			ScenarioFramework.WAPI.Response strAnswer = new ScenarioFramework.WAPI.Response();
			string strResponse;
			strResponse = ScenarioFramework.WAPI.Role.GetRoles(ref strAnswer);

			if (strAnswer.iCode == 0)
			{
				XMLRoles AuxRoles = new XMLRoles();
				AuxRoles.Parse(strResponse);


				
				// Get all roles for All Roles Listbox
				lstAllRoles.DataSource = AuxRoles;
				lstAllRoles.DataTextField = "Name";
				lstAllRoles.DataValueField = "Key";

				lstAllRoles.DataBind();
				
				/**
								for (int i=0;i<AuxRoles.Count;i++)
								{
									XMLRole Rol = (XMLRole)AuxRoles[i];
									ListItem Item = new ListItem(Rol.sName,Rol.sName);
									lstAllRoles.Items.Add(Item);

								}
				**/

			}
			else
				throw (new ArgumentException("ERROR - USERROLECONTROL - GET ROLES - " + strAnswer.sMsg));
			
		}

		private void BindAllUsers()
		{			
			
			// Execute WAPI Command
			ScenarioFramework.WAPI.Response strAnswer = new ScenarioFramework.WAPI.Response();
			string strResponse;
			strResponse = ScenarioFramework.WAPI.User.GetUsers(ref strAnswer);

			if (strAnswer.iCode == 0)
			{

				XMLUsers AuxUsers = new XMLUsers();
				AuxUsers.Parse(strResponse);
				
				// Get all users for All Users List Box
				lstAllUsers.DataSource = AuxUsers;
				lstAllUsers.DataTextField = "Name";
				lstAllUsers.DataValueField = "Key";
				lstAllUsers.DataBind();
				
				/**
								for (int i=0;i<AuxUsers.Count;i++)
								{
									XMLUser Use = (XMLUser)AuxUsers[i];
									ListItem Item = new ListItem(Use.sName,Use.sName);
									lstAllUsers.Items.Add(Item);

								}
				**/
			}
			else
				throw (new ArgumentException("ERROR - USERROLECONTROL - GET USERS - " + strAnswer.sMsg));
			
		}



		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				BindUsersRoles();
			}
		}

		protected void btnAddUser_Click(object sender, System.EventArgs e)
		{
			if (lstAllUsers.SelectedIndex > -1) 
			{
				lstSelectedUsers.SelectedIndex = -1;
				lstSelectedUsers.Items.Add( lstAllUsers.SelectedItem );
				lstAllUsers.Items.Remove(lstAllUsers.SelectedItem);

				
			}
		}

		protected void btnAddRole_Click(object sender, System.EventArgs e)
		{
			if (lstAllRoles.SelectedIndex > -1) 
			{
				lstSelectedRoles.SelectedIndex = -1;
				lstSelectedRoles.Items.Add( lstAllRoles.SelectedItem );
				lstAllRoles.Items.Remove(lstAllRoles.SelectedItem);

				

			}
		}

		protected void btnRemoveRole_Click(object sender, System.EventArgs e)
		{
			if (lstSelectedRoles.SelectedIndex > -1) 
			{
				lstAllRoles.SelectedIndex = -1;
				lstAllRoles.Items.Add( lstSelectedRoles.SelectedItem );
				lstSelectedRoles.Items.Remove(lstSelectedRoles.SelectedItem);

			}		
		}

		protected void btnRemoveUser_Click(object sender, System.EventArgs e)
		{
			if (lstSelectedUsers.SelectedIndex > -1) 
			{
				lstAllUsers.SelectedIndex = -1;
				lstAllUsers.Items.Add( lstSelectedUsers.SelectedItem );
				lstSelectedUsers.Items.Remove(lstSelectedUsers.SelectedItem);

			}	
		}

		public string UsersIn ()
		{
			if (lstSelectedUsers.Items.Count >= 1) 
			{
				XMLUsers users = new XMLUsers();

				for (int i=0;i<lstSelectedUsers.Items.Count;i++)
				{
					// Execute WAPI Command
					ScenarioFramework.WAPI.Response strAnswer = new ScenarioFramework.WAPI.Response();
					string strResponse;
					strResponse = ScenarioFramework.WAPI.User.GetUser(lstSelectedUsers.Items[i].Value.ToString(),ref strAnswer);
					if (strAnswer.iCode==0)
					{
					
						XMLUser user = new XMLUser();
						user.Parse(strResponse);

						users.Add(user);

					}
					else
						throw (new ArgumentException("ERROR - USERROLECONTROL - GET USER - " + strAnswer.sMsg));

				}

				return users.GetXML();

			}
			else 
				return "<Users></Users>";

		}

		public string RolesIn ()
		{
			if (lstSelectedRoles.Items.Count >= 1) 
			{
				XMLRoles roles = new XMLRoles();

				for (int i=0;i<lstSelectedRoles.Items.Count;i++)
				{
					// Execute WAPI Command
					ScenarioFramework.WAPI.Response strAnswer = new ScenarioFramework.WAPI.Response();
					string strResponse;
					strResponse = ScenarioFramework.WAPI.Role.GetRolesByKey(lstSelectedRoles.Items[i].Value.ToString(),ref strAnswer);
					if (strAnswer.iCode==0)
					{
						
						XMLRole role = new XMLRole();
						role.Parse(strResponse);

						roles.Add(role);

					}
					else
						throw (new ArgumentException("ERROR - USERROLECONTROL - GET USER - " + strAnswer.sMsg));

				}

				return roles.GetXML();

			}
			else 
				return "<Roles></Roles>";
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
